<br />
<?php
if (!isset ($update))
	$update = false;
?>
<div class="yiiForm">
<table class="dataGrid" id="parameter_table">
  <thead>
  <tr>
<?php foreach($model->headers as $header): ?>
    <th><?php


$this->widget('application.modules.admin.widgets.ParameterHeaderWidget', array (
	"header" => $header,
	"update" => $update
));
//echo $header['label']; 
?></th>
<?php


endforeach;
if ($update) {
?>
    <th><?php echo CHtml::link('New column','#', array ('onclick' => '$("#column_dialog").dialog({ title: "New column", header_mnemonic: null }).dialog("open"); return false;')); ?></th>
<?php


}
?>
  </tr>
  </thead>
  <tbody>
<?php foreach($model->values as $n=>$row): ?>
  <tr class="<?php echo $n%2?'even':'odd';?>">
	<?php foreach($model->headers as $header): ?>
    <td><?php


$this->widget('application.modules.admin.widgets.ParameterDisplayWidget', array (
	"name" => $header->mnemonic,
	"type" => $header->type,
	"model" => $row,
	"is_editor" => $update
));
?></td>
	<?php


endforeach;
if ($update) {
?>
    <td>delete</td>
<?php


}
?>
  </tr>
<?php endforeach; ?>
  </tbody>
</table>
</div> <!-- yiiForm -->

<?php


if ($update) {
	$this->beginWidget('zii.widgets.jui.CJuiDialog', array (
		'id' => 'column_dialog',
		// additional javascript options for the dialog plugin
		'options' => array (
			'title' => 'Add new column',
			'autoOpen' => false,
			'resizable' => false,
			'width' => 600,
			'height' => 400
		)
	));
	$f = new ParameterHeaderForm();
	echo '<div class="yiiFormDlg">' .
	'<div class="simple">' .
	CHtml::label($f->getAttributeLabel('mnemonic'), 'dlg_mnemonic') .
	'</div>' .
	CHtml::textField('dlg_mnemonic', '', array (
		'id' => 'dlg_mnemonic',
		'size' => '10',
		'maxlength' => '10',
		'class' => 'text'
	)) .

	'<div class="simple">' .
	CHtml::label($f->getAttributeLabel('label'), 'dlg_label') .
	'</div>' .
	CHtml::textField('dlg_label', '', array (
		'id' => 'dlg_label',
		'size' => '50',
		'maxlength' => '100',
		'class' => 'text'
	)) .

	'<div class="simple">' .
	CHtml::label($f->getAttributeLabel('index'), 'dlg_index') .
	'</div>' .
	CHtml::textField('dlg_index', '', array (
		'id' => 'dlg_index',
		'size' => '5',
		'maxlength' => '5',
		'class' => 'text'
	)) .

	'<div class="simple">' .
	CHtml::label($f->getAttributeLabel('type'), 'dlg_type') .
	'</div>' .
	CHtml::listBox('dlg_type', '', array (
		'integer' => Yii::t('type', 'integer'),
		'float' => Yii::t('type', 'flat'),
		'string' => Yii::t('type', 'string'),
		'date' => Yii::t('type', 'date'),
		'datetime' => Yii::t('type', 'datetime'),
		'hour' => Yii::t('type', 'time'),
		'boolean' => Yii::t('type', 'boolean'),
		'text' => Yii::t('type', 'text')
	), array (
		'id' => 'dlg_type',
		'size'=>1,
		'class' => 'text'
	)) .

	'<div class="simple">' .
	CHtml::label($f->getAttributeLabel('key'), 'dlg_key') .
	'</div>' .
	CHtml::checkBox('dlg_key', false, array (
		'id' => 'dlg_key'
	)) .
	'';

	echo '<div class="action">' .
	CHtml::button(Yii::t('type', 'OK'), array('id' => 'dlg_ok')) .
	"&nbsp;" .
	CHtml::button(Yii::t('type', 'Cancel'), array('id' => 'dlg_cancel')) .
	'</div></div>' .
	CHtml::hiddenField('dlg_old_mnemonic', '', array (
		'id' => 'dlg_old_mnemonic'
	)) .
	CHtml::hiddenField('dlg_old_index', '', array (
		'id' => 'dlg_old_index'
	)) 
	;
	$this->endWidget('zii.widgets.jui.CJuiDialog');

	Yii::app()->clientScript->registerScriptFile(CHtml::normalizeUrl(array("headerJs", 'mnemonic' => $model->param_mnemonic)));

	Yii::app()->clientScript->registerScript('column_dialog_openned', '$("#column_dialog").bind("dialogopen", function(event, ui) {
var mnemonic = $("#column_dialog").dialog("option", "header_mnemonic"); 
var header = undefined;
if (mnemonic !== null && mnemonic !== undefined)
	header = headers.GetHeader(mnemonic);
	
if (header === undefined)
{
	$("#dlg_mnemonic").attr("value", "");
	$("#dlg_old_mnemonic").attr("value", "");
	$("#dlg_label").attr("value", "");
	$("#dlg_index").attr("value", $("#parameter_table > thead > tr > th").length);
	$("#dlg_old_index").attr("value", -1);
	$("#dlg_type").attr("value", "integer").attr(":readonly", false);
	$("#dlg_key").attr(":checked", $("#parameter_table > thead > tr > th").length == 1);
}else{
	$("#dlg_mnemonic").attr("value", header.mnemonic);
	$("#dlg_old_mnemonic").attr("value", header.mnemonic);
	$("#dlg_label").attr("value", header.label);
	$("#dlg_index").attr("value", header.index);
	$("#dlg_old_index").attr("value", header.index);
	$("#dlg_type").attr("value", header.type).attr(":readonly", true);
	$("#dlg_key").attr(":checked", header.key);
}

		});');

	Yii::app()->clientScript->registerScript('ok_dialog_button_clicked', ' 
$("#dlg_ok").bind("click", function()
{
	var header = new Header(
		$("#dlg_mnemonic").attr("value"),
		$("#dlg_label").attr("value"),
		$("#dlg_index").attr("value"),
		$("#dlg_type").attr("value"),
		$("#dlg_key").is(":checked")
	);
	
	try {
		var old_index = $("#dlg_old_index").attr("value");
		var old_mnemonic = $("#dlg_old_mnemonic").attr("value");
		
		if(old_index == -1)
		{
			if (header.index > $("#parameter_table > thead > tr > th").length)
			{
				header.index = $("#parameter_table > thead > tr > th").length;
		   		$("#dlg_index").attr("value", $("#parameter_table > thead > tr > th").length);
			}
				
			headers.AddHeader(header);
			var idx = $("#parameter_table > thead > tr > th").length;
			html  = "<th>";
			html += "<a href=\"#\" onclick=\"$(\'#column_dialog\').dialog({ title: \'"+header.label+"\', header_mnemonic: \'"+header.mnemonic+"\' }).dialog(\'open\'); return false;\"><span id=\"header_" + header.mnemonic + "\">"+header.label+"</span></a>";
			html += "<input type=\"hidden\" name=\"ParameterHeaderForm["+idx+"][mnemonic]\" value=\""+header.mnemonic+"\" id=\"ParameterHeaderForm_"+header.index+"_mnemonic\" />";
			html += "<input type=\"hidden\" name=\"ParameterHeaderForm["+idx+"][label]\" value=\""+header.label+"\" id=\"ParameterHeaderForm_"+header.index+"_label\" />";
			html += "<input type=\"hidden\" name=\"ParameterHeaderForm["+idx+"][index]\" value=\""+header.index+"\" id=\"ParameterHeaderForm_"+header.index+"_index\" />";
			html += "<input type=\"hidden\" name=\"ParameterHeaderForm["+idx+"][type]\" value=\""+header.type+"\" id=\"ParameterHeaderForm_"+header.index+"_type\" />";
			html += "<input type=\"hidden\" name=\"ParameterHeaderForm["+idx+"][key]\" value=\""+header.key+"\" id=\"ParameterHeaderForm_"+header.index+"_key\" />";
			html += "</th>";
			$("#parameter_table > thead > tr > th:nth-child("+header.index+")").before(html)
		}else{
			if (header.index > $("#parameter_table > thead > tr > th").length-1)
			{
				header.index = $("#parameter_table > thead > tr > th").length-1;
		   		$("#dlg_index").attr("value", $("#parameter_table > thead > tr > th").length-1);
			}
				
			if (header.mnemonic != old_mnemonic)
			{
				// Mnemonic has changed. 
				// Check it is unique !
				if (headers.GetHeader(header.mnemonic) !== undefined)
				{
					throw new HeaderException("mnemonic", "Parameter already exists");
				}
				
				// We can update all values for this column the span title id...
				// TODO: update values
				// title :
				$("#header_"+old_mnemonic).attr("id","#header_"+header.mnemonic)
			}
			if (header.index != old_index)
			{
				// we move column...
				var old_h = $("#parameter_table > thead > tr > th:nth-child("+(old_index)+")");
				var new_h = $("#parameter_table > thead > tr > th:nth-child("+(header.index)+")");
				new_h.insertAfter(old_h);
				//$("#parameter_table > th:nth-child("+old_index+")").insertBefore(new_h);
			}
			
			$("#header_"+header.mnemonic).html(header.title);
		}
			
		$("#column_dialog").dialog("close");
	} catch(ex){
		$("#dlg_"+ex.field).focus();
		alert(ex.message);
	}
});
');
}
?>




